<template>
  <div class="todo-item" :class="{ completed: todo.completed }">
    <span>{{ todo.text }}</span>
    <div class="actions">
      <button @click="handleToggle">
        {{ todo.completed ? '未完成' : '完成' }}
      </button>
      <button @click="handleDelete" class="delete-btn">
        删除
      </button>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue';

// 定义从父组件接收的属性
const props = defineProps({
  todo: {
    type: Object,
    required: true,
    // 任务对象的结构
    properties: {
      id: { type: Number },
      text: { type: String },
      completed: { type: Boolean }
    }
  }
});

// 定义要向父组件发送的事件
const emit = defineEmits(['toggle-todo', 'delete-todo']);

// 处理完成/未完成切换
const handleToggle = () => {
  emit('toggle-todo', props.todo.id);
};

// 处理删除任务
const handleDelete = () => {
  emit('delete-todo', props.todo.id);
};
</script>

<style scoped>
.todo-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  margin-bottom: 8px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

/* 已完成任务的样式 */
.completed {
  text-decoration: line-through;
  color: #888;
}

.actions {
  display: flex;
  gap: 8px;
}

button {
  padding: 4px 8px;
  border: none;
  cursor: pointer;
}

.delete-btn {
  background-color: #ff4444;
  color: white;
}
</style>
    